//
//  HTTravelPicCell.m
//  HeartTravel
//
//  Created by lanou on 15/10/31.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTTravelPicCell.h"
#import "UIImageView+WebCache.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"

@implementation HTTravelPicCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 20)];
        _timeLabel.textColor = [UIColor lightGrayColor];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        
        _travelPic = [[UIImageView alloc] init];
        
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.font = [UIFont systemFontOfSize:12];
        _contentLabel.numberOfLines = 0;
        
        [self.contentView addSubview:_timeLabel];
        [self.contentView addSubview:_travelPic];
        [self.contentView addSubview:_contentLabel];
    }
    
    return self;
}

- (void)tapImage:(UITapGestureRecognizer *)tap
{
//    NSInteger count = _commentMD.status_photoArr.count;
    
    //1.封装图片数据
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:0];
    
//    for (int i = 0 ; i < count; i ++) {
//        
//        HTTripDeattillCommentPhotoModel *model = [[HTTripDeattillCommentPhotoModel alloc]init];
//        model = _commentMD.status_photoArr[i];
    
    HTWaypointsModel *model = [[HTWaypointsModel alloc] init];
    model = _configureModel;
    
    MJPhoto *photo = [[MJPhoto alloc]init];
        
//        NSString *imageUrlStr = [self normolWithString:model.img_url];
    
    photo.url = [NSURL URLWithString:model.photo];
        
    if ([[self.contentView viewWithTag: 0] isKindOfClass:[UIImageView class]]) {
            
        photo.srcImageView = (UIImageView *)[self.contentView viewWithTag: 0];
    }
        
    [photos addObject:photo];
        
//    }
    
    //2.显示放大的图片
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    //这里是imageveiw的个数,注意tag值的添加与数组的个数相一致,不然会出现数组越界的问题
    browser.currentPhotoIndex = tap.view.tag;//弹出相册时显示
    //注意这里是装imageview的数组
    browser.photos = photos;//设置所有的图片
    
    [browser show];
    
}

- (void)setConfigureModel:(HTWaypointsModel *)configureModel {
    
    _configureModel = configureModel;
    _timeLabel.text = configureModel.local_time;
  
    CGFloat photo_h = (SCREEN_WIDTH - 20)*configureModel.photo_h.floatValue/configureModel.photo_w.floatValue;
    _travelPic.frame = CGRectMake(10, 60, SCREEN_WIDTH - 20, photo_h);
    
    [_travelPic sd_setImageWithURL:[NSURL URLWithString:configureModel.photo]];
    _travelPic.tag = 0;
    _travelPic.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
    [_travelPic addGestureRecognizer:tap];
    _contentLabel.frame = CGRectMake(10, 90 + photo_h, SCREEN_WIDTH - 20, [HTTravelPicCell heightForString:configureModel.text]);
    _contentLabel.text = configureModel.text;
   
}

+ (CGFloat)heightForString:(NSString *)string {
    
    CGRect bounds = [string boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    
    return bounds.size.height;
}

+ (CGFloat)heightForCellWithModel:(HTWaypointsModel *)model {
    CGFloat height = 0;
   
    height = (SCREEN_WIDTH - 20)*model.photo_h.floatValue/model.photo_w.floatValue + [self heightForString:model.text] + 100;
   
    
    return height;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
